package com.sise.config;

import com.sise.common.dto.OrderDTO;
import com.sise.common.interfaces.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;


/**
 * @author idea
 * @data 2019/10/9
 */
@Component
@Slf4j
public class RedisMessageListener extends KeyExpirationEventMessageListener {

    @Autowired
    private OrderService orderService;

    /**
     * 监听
     *
     * @param listenerContainer
     */
    public RedisMessageListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件，进行数据处理
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredOrderNumber = message.toString();
        OrderDTO orderDTO=new OrderDTO();
        //设置订单为过期
        orderDTO.setOrderStatus(1);
        orderDTO.setOrderNumber(expiredOrderNumber);
        orderService.updateExpireOrder(orderDTO);
        System.out.println("【订单过期处理】orderNumber:"+expiredOrderNumber);
    }

}
